const key = '4IUBZ-LDUWF-SCVJP-JYV6W-D3VEJ-OHF67'; //使用在腾讯位置服务申请的key
const referer = '流连忘返游西域'; //调用插件的app的名称
const plugin = requirePlugin('routePlan');
const chooseLocation = requirePlugin('chooseLocation');

Page({
  data: {
    //起点
    origin: {
      address: "",
      city: "",
      latitude: "",
      longitude: "",
      name: ""
    },
    //终点
    destination: {
      address: "",
      city: "",
      latitude: "",
      longitude: "",
      name: ""
    },

    type: ""

  },


  onLoad: function (options) {
    if (options.name == null){
      return;
    }
    let that = this
    that.data.destination.name = options.name
    that.data.destination.latitude = options.latitude
    that.data.destination.longitude = options.longitude
    that.setData({
      destination: that.data.destination
    })
  },

  onShow() {
    // 页面被展示
    let type = this.data.type
    if (type == '') {
      return;
    }

    let location = chooseLocation.getLocation(); // 如果点击确认选点按钮，则返回选点结果对象，否则返回null
    debugger
    if (type == 'origin') {
      this.setData({
        origin: location
      })
    } else if (type == 'destination') {
      this.setData({
        destination: location
      })
    }
  },

  getLocal: function(event) {
    debugger
    let _this = this;
    _this.setData({
      type: event.target.dataset.type
    })
    let category = '旅游景点,美食,购物';
    wx.navigateTo({
      url: `plugin://chooseLocation/index?key=${key}&referer=${referer}&category=${category}`
    });
  },
  search: function() {
    if (this.data.destination.name == "" ||
      this.data.destination.latitude == "" ||
      this.data.destination.longitude == "") {
      return;
    }

    let startPoint = JSON.stringify({ //终点
      'name': this.data.origin.name,
      'latitude': this.data.origin.latitude,
      'longitude': this.data.origin.longitude
    });
    let endPoint = JSON.stringify({ //终点
      'name': this.data.destination.name,
      'latitude': this.data.destination.latitude,
      'longitude': this.data.destination.longitude
    });
    let url = 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&startPoint=' + startPoint + '&endPoint=' + endPoint
    if (this.data.origin.name == "" ||
      this.data.origin.latitude == "" ||
      this.data.origin.longitude == "") {
      url = 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint + '&navigation=1';
    }
    wx.navigateTo({
      url: url
    });
  },

})